package com.uyayo.framework.pagehelper.util;

import com.uyayo.framework.pagehelper.pojo.OrderBy;

import java.util.List;

public class OrderByUtil {
	
	public String getOrderBy(List<OrderBy> orderBys, String sort, String order){
		StringBuffer stringBuffer = new StringBuffer("");

		if(orderBys==null || sort == null || order==null){
			return stringBuffer.toString();
		}
		
		String [] sorts = sort.split(",");
		String [] orders = order.split(",");
		if(sorts==null ||orders==null || orders.length< 1 ||  sorts.length < 1 || sorts.length != orders.length){
			return stringBuffer.toString();
		}
		if(orderBys==null || orderBys.size()<1){
			return stringBuffer.toString();
		}
		for (int i = 0; i < sorts.length || i< orders.length; i++) {
			for (int j = 0; j < orderBys.size(); j++) {
				if(sorts !=null && sorts[i].equals(orderBys.get(j).getProperty()) && orders[i] !=null && !"".equals(orders[i])){
					if(i == sorts.length-1){
						stringBuffer.append(orderBys.get(j).getColumn()).append(" ").append(orders[i]);
					}else{
						stringBuffer.append(orderBys.get(j).getColumn()).append(" ").append(orders[i]).append(" , ");
					}
				}
			}
		}
		return stringBuffer.toString();
	}
}
